/*
namespace Triumph
{
    partial class Form1
    {
        
    }
}
*/

using System.Collections;
using System.Text.RegularExpressions;
using System;
using System.IO;

namespace Triumph
{
    partial class Form1
    {
        private string Renamer(Hashtable info) { return Renamer(info, textBoxFileRenameFormat.Text); }
        /// <summary>
        /// A function that takes info hashtable with named keys as input aswell as a format string
        /// </summary>
        /// <param name="info"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        private string Renamer(Hashtable info, string format)
        {
            Regex rxFormat = new Regex(@"\[([^\]]*)\]");
            Regex rxNum = new Regex(@"\((\d+)\)");
            string result = string.Empty;
            string fixing = comboBoxRenameFix.Text;

            /* We need to keep them in order!!! */
            foreach (Match m in rxFormat.Matches(format))
            {
                /*Title:Prefix:PadLeft,2
                 * Main parts are:
                 * Artist
                 * Album
                 * #
                 * Title
                 * ext */
                string part = m.Groups[1].Value;
                string[] options = part.Split(char.Parse(":"));

                string name = options[0].ToLower();
                if (name == "#")
                    name = "track";

                bool optional = false;
                bool prefix = false;
                bool sufix = false;
                bool albumdependant = false;
                string casing = comboBoxRenameCasing.Text.ToLower();
                string pad = string.Empty;

                if (options.GetUpperBound(0) > 0)
                {
                    // Find options of this format string
                    for (int i = 1; i <= options.GetUpperBound(0); i++)
                    {
                        string opt = options[i].ToLower();

                        if (opt == "optional")
                            optional = true;
                        else if (opt == "prefix")
                            prefix = true;
                        else if (opt == "sufix")
                            sufix = true;
                        else if (opt == "albumdependant")
                            albumdependant = true;
                        else if (opt.EndsWith("case"))
                            casing = opt;
                        else if (opt.EndsWith("pad"))
                            pad = opt;
                        /*else
                            Console.WriteLine("Missed option: " + opt);
                         This happens all the time when editing the format string so capture that
                         */
                    }
                    /* Caught into variables:
                     * optional
                     * prefix, suffix
                     * left(3)pad, (#)pad, right(#)pad
                     * lowercase, uppercase, nocase, propercase
                     */
                }

                /* If we don't have the translation for something and
                 * it's not optional we can't continue so end it here */
                string temp = string.Empty;
                if (!info.ContainsKey(name) && !optional)
                {
                    return string.Empty;
                }
                else if (info.ContainsKey(name))
                {
                    /* We really need this string */
                    temp = info[name].ToString();

                    if (checkBoxReplaceFixInNames.Checked)
                    {
                        temp = temp.Replace(fixing.Trim(), " ");
                    }
                    if (ReplaceUnderscoreInNames.Checked)
                    {
                        temp = temp.Replace("_", " ");
                    }

                    /* CASING */
                    if (IsValid(casing))
                    {
                        switch (casing)
                        {
                            //case "nocase"
                            case "lowercase":
                                temp = temp.ToLower();
                                break;
                            case "uppercase":
                                temp = temp.ToUpper();
                                break;
                            case "propercase":
                                temp = ProperCase(temp);
                                break;
                        }
                    }

                    /* Padding */
                    if (IsValid(pad))
                    {
                        // Left(3)Pad
                        // Right(2)Pad
                        int pads = 0;
                        int.TryParse(rxNum.Match(pad).Groups[1].Value, out pads);
                        char padChar = char.Parse(" ");
                        
                        if (pad.StartsWith("left"))
                        {
                            if (name == "track")
                                padChar = char.Parse("0");

                            temp = temp.PadLeft(pads, padChar);
                        }
                        else if (pad.StartsWith("right"))
                        {
                            temp = temp.PadRight(pads, padChar);
                        }
                    }

                    /* Prefix/Sufix */
                    if (prefix)
                        temp = fixing + temp;
                    if (sufix)
                        temp = temp + fixing;

                    /* Dependencies - only found the need for album dependencies (track) so only built that so far */
                    if (albumdependant && !info.ContainsKey("album"))
                        temp = string.Empty;
                    // Add Track dependant
                }

                //build our string here
                result += temp;
            }

            // Add Extension here :)
            if (info.ContainsKey("ext"))
            {
                string extension = info["ext"].ToString().ToLower();
                if (!extension.StartsWith("."))
                {
                    extension = "." + extension;
                }
                result += extension;
            }

            // Remove dual white spaces
            while (result.Contains("  "))
            {
                result = result.Replace("  ", " ");
            }

            // Remove invalid chars
            foreach (char chr in Path.GetInvalidFileNameChars())
            {
                result = result.Replace(chr.ToString(), "");
            }

            return result;
        }

        private string StripExtensionFromFileName(string filename, string extension)
        {
            return filename.Substring(0, (filename.Length - extension.Length));
        }

        private bool IsValid(string s)
        {
            if (s == null)
                return false;
            if (s == "")
                return false;
            if (s == string.Empty)
                return false;
            if (s == "nocase")
                return false;

            return true;
        }
    }
}